﻿using Comon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;

namespace QC {
	public partial class Form1 : Form {
		public Form1() {
			InitializeComponent();
		}

		/// <summary>
		/// 开始操作
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnOK_Click(object sender, EventArgs e) {
			//定义颜色信息
			string colStr = string.Empty;//为了手机端扫描结果根据此字段生成彩码
			//减少服务器的使用 即用户扫描一次生成彩码图片 流的形式展示  无需服务器存储图片

			//五色集合
			List<Color> colorList = AppConst.GetColorList();

			int row = dataGridView1.Rows.Count;//得到总行数 
			List<Brush> brushList = new List<Brush>();
			QrCodeHelper codeCreate = new QrCodeHelper();
			if (row > 0) {
				for (int i = 0; i < row - 1; i++)//得到总行数并在之内循环    
                {
					string urlinfo = dataGridView1.Rows[i].Cells[0].Value.ToString();

					Random ra = new Random();
					int[] colorInts = new int[4];
					colStr = string.Empty;
					for (var w = 0; w < 4; w++) {
						var rand = ra.Next(0, 4);
						colorInts[w] = rand;
						colStr += rand + ",";
					}
					string content =AppConst.codeUrl+ urlinfo.TrimEnd(',') + "-" + colStr.TrimEnd(',');
					 
					brushList.Clear();
					Image img1 = codeCreate.Create_ImgCode(content, 4, colorList[colorInts[0]],200);
					//创建对应颜色
					var b1 = new SolidBrush(colorList[colorInts[0]]);
					brushList.Add(b1);

					Image img2 = codeCreate.Create_ImgCode(content, 4, colorList[colorInts[1]], 200);

					var b2 = new SolidBrush(colorList[colorInts[1]]);
					brushList.Add(b2);

					Image img3 = codeCreate.Create_ImgCode(content, 4, colorList[colorInts[2]], 200);

					var b3 = new SolidBrush(colorList[colorInts[2]]);
					brushList.Add(b3);

					Image img4 = codeCreate.Create_ImgCode(content, 4, colorList[colorInts[3]], 200);

					var b4 = new SolidBrush(colorList[colorInts[3]]);
					brushList.Add(b4);

					//分别切除四个图片的四个角 获得新的四个小图片信息
					Image img5 = codeCreate.CutImage(img1, 0, 0, img1.Width / 2, img1.Height / 2);
					Image img6 = codeCreate.CutImage(img2, img1.Width / 2, 0, img2.Width / 2, img2.Height / 2);
					Image img7 = codeCreate.CutImage(img3, 0, img1.Height / 2, img3.Width / 2, img3.Height / 2);
					Image img8 = codeCreate.CutImage(img4, img1.Width / 2, img2.Height / 2, img4.Width / 2, img4.Height / 2);
					img1.Dispose(); img1 = null;
					img2.Dispose(); img2 = null;
					img3.Dispose(); img3 = null;
					img4.Dispose(); img4 = null;
					////拼接四张图片信息
					Image img9 = codeCreate.JoinImage(img5, img6, img7, img8);
					codeCreate.SaveImage2File(AppConst.UpLoadUrl + "//" + (i + 1) + ".png", img9,95);
					 
					img5.Dispose(); img5 = null;
					img6.Dispose(); img6 = null;
					img7.Dispose(); img7 = null;
					img8.Dispose(); img8 = null;

					//////创建对应的颜色的彩色字体信息
					 Image imgFont = codeCreate.ColorFont(brushList, urlinfo.TrimEnd(','), 5);
					// codeCreate.SaveImage2File(AppConst.FontUpload + "//" + (i + 1) + ".png", imgFont,95);
					 imgFont.Save(AppConst.FontUpload + "//" + (i + 1) +"-" + urlinfo.TrimEnd(',') + ".png", ImageFormat.Png);
					////图片拼接
					//codeCreate.JoinImage(imgFont, img9, "D:\\Upload\\\\fina" + i + ".jpg");

				}


			}


			MessageBox.Show("成功操作！");


		}
		/// <summary>
		/// 导码对话框
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void importCode_Click(object sender, EventArgs e) {
			DataTable tbl = new DataTable();
			tbl.Columns.Add("UrlInfo", typeof(string));
			//定义一个文件打开控件
			OpenFileDialog ofd = new OpenFileDialog();
			if (ofd.ShowDialog() == DialogResult.OK) {
				//成功选择文件过后操作
				//获取用户选择的文件完整路径
				string filePath = ofd.FileName;
				//获取对话框中所选文件的文件名和扩展名，文件名不包括路径
				string fileName = ofd.SafeFileName;
				StreamReader sr = new StreamReader(filePath, Encoding.UTF8);
				String line;
				while ((line = sr.ReadLine()) != null) {
					DataRow dr = tbl.NewRow();
					dr[0] = line;
					tbl.Rows.Add(dr);
				}

				this.dataGridView1.DataSource = tbl;

			}
		}
	}
}
